home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / rayshade / graphtal.lzh / Graphtal.Amiga / LSystem.h < prev    next >
C/C++ Source or Header  |  1992-11-20  |  3KB  |  135 lines

  1. /*
  2.  * LSystem.h - class definitions for classes ProductionSlot, Table 
  3.  *             and LSystem.
  4.  *
  5.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  6.  *                     University of Berne, Switzerland
  7.  * All rights reserved.
  8.  *
  9.  * This software may be freely copied, modified, and redistributed
  10.  * provided that this copyright notice is preserved on all copies.
  11.  *
  12.  * You may not distribute this software, in whole or in part, as part of
  13.  * any commercial product without the express consent of the authors.
  14.  *
  15.  * There is no warranty or other guarantee of fitness of this software
  16.  * for any purpose.  It is provided solely "as is".
  17.  *
  18.  */
  19.  
  20. #ifndef LSystem_H
  21. # define LSystem_H
  22.  
  23. #include "rcString.h"
  24. #include "list.h"
  25. #include "Hash.h"
  26. #include "Production.h"
  27. #include "Module.h"
  28.  
  29. //___________________________________________________________ Table Hashing
  30.  
  31. unsigned int modHash(const char*, long);
  32.  
  33. //___________________________________________________________ ProductionSlot
  34. //
  35. // a ProductionSlot is the basic element of a Table and is the 'home' of
  36. //  the productions with the same module name and the same number of
  37. //  arguments
  38.  
  39. class ProductionSlot
  40. {
  41. friend class Table;
  42. public:
  43.   ProductionSlot(Production*);
  44.   ~ProductionSlot();
  45.  
  46.   void append(Production*);
  47.   int match(Production*) const;
  48.   int match(Module*) const;
  49.  
  50. private:
  51.   Name name;    // module name
  52.   int argCount; // number of arguments
  53.   ProductionList* productions; // productions associated with the
  54.                                // ProductionSlot
  55. };
  56.  
  57. //___________________________________________________________ Table
  58. //
  59. // A Table consists of a list of productions which are stored in
  60. //  a hashed array to achieve fast module-production matching
  61.  
  62. const int HASH_ARRAY_SIZE = 503;
  63.  
  64. class Table
  65. {
  66. public:
  67.   Table(const Name&, ProductionList*);
  68.   ~Table();
  69.  
  70.   const Name& name() const;
  71.   ModuleList* execute(ModuleList*, int);
  72.   friend ostream& operator<<(ostream&, const Table&);
  73.  
  74. private:
  75.   Name _name; // table name
  76.   ProductionList* productions;  // the productions stored in the table
  77.   ProductionSlot* pslots[HASH_ARRAY_SIZE];
  78.   ModuleList* result;
  79.  
  80.   void rexecute(Module*, int);
  81.   ModuleList* apply(Module*); 
  82. };
  83.  
  84. typedef Table* TablePtr;
  85. declareList(TableList, TablePtr);
  86.  
  87. inline const Name& Table::name() const { 
  88.   return _name; 
  89. }
  90.  
  91. //___________________________________________________________ LSystem
  92. //
  93. // A L-System consists of a derivation list, an axiom and 
  94. //  productions which are grouped in tables.
  95. //  The derivation list gives the order of tables to be applied 
  96. //  to the axiom (= the initial module string)
  97.  
  98. struct DerivationItem
  99. {
  100.   DerivationItem(Table* t, int s)
  101.   : table(t), steps(s) {}
  102.   
  103.   Table* table;
  104.   int steps;
  105. };
  106.  
  107. ostream& operator<<(ostream&, const DerivationItem&);
  108.  
  109. typedef DerivationItem* DerivationItemPtr;
  110. declareList(DerivationList, DerivationItemPtr);
  111.  
  112. class LSystem
  113. {
  114. public:
  115.   LSystem(const Name&, TableList*, ProdModuleList*, DerivationList*);
  116.   ~LSystem();
  117.  
  118.   ModuleList* execute();
  119.   rcString getName() const;
  120.   friend ostream& operator<<(ostream&, const LSystem&);
  121.  
  122. private:
  123.   Name name;                  // the name of the l-system
  124.   TableList* tables;          // tables of the l-system
  125.   ProdModuleList* axiom;      // the axiom
  126.   DerivationList* derivation; // list of tables to be applied
  127. };
  128.  
  129. inline rcString LSystem::getName() const { 
  130.   return rcString((const char*)name); 
  131. }
  132.  
  133. #endif // LSystem_H
  134.  
  135.